package com.zbec.mdrs.web;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zbec.mdrs.entity.loan.Loan;
import com.zbec.mdrs.service.FileService;
import com.zbec.mdrs.vo.ExportVo;
import org.apache.commons.io.FileUtils;
import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value = "/file")
public class FileController extends BaseController {

	@Autowired
	private FileService fileService;

	/**
	 */
	@RequestMapping(value = "/down")
	public void download(@RequestParam(value = "fileName", defaultValue = "") String fileName,
						 @RequestParam(value = "systemType", defaultValue = "") String systemType,
						 @RequestParam(value = "url", defaultValue = "") String url,
			HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		OutputStream os = null;
		try {
			os = new BufferedOutputStream(response.getOutputStream());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		request.setCharacterEncoding("UTF-8");
		String path=url;
		String suffix="";
		if(url.equals("")){
			if(systemType.equals("temp")){
				path=request.getSession().getServletContext().getRealPath("/template");
			}else{
				path=request.getSession().getServletContext().getRealPath("/downLoad");
			}
			path=path+File.separator+fileName+".xls";
			suffix=".xls";
		}

		File f = new File(path);
		String fileChineseName = fileName;
		if(systemType.equalsIgnoreCase("loan")){
			fileChineseName = "贷款数据";
		}else if(systemType.equalsIgnoreCase("discount")){
			fileChineseName = "贴现数据";
		}else if(systemType.equalsIgnoreCase("drafts")){
			fileChineseName = "银票数据";
		}else if(systemType.equalsIgnoreCase("entrustedLoan")){
			fileChineseName = "委托贷款数据";
		}else if(systemType.equalsIgnoreCase("guarantee")){
			fileChineseName = "担保数据";
		}
		try {
			response.reset();
			if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > -1)
			{
				response.setHeader("Content-Disposition", "attachment; filename="
						+ new String((fileChineseName+".xls").getBytes("UTF-8"), "ISO-8859-1"));
			}else{
				response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileChineseName, "UTF-8")+suffix );
			}
			response.setContentType("application/vnd.ms-excel");
			os.write(FileUtils.readFileToByteArray(f));
			os.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
//			f.delete();
		}
	}
	@RequestMapping(value = "/exportExcel",produces="application/json;charset=utf-8")
	@ResponseBody
	public String exportExcel(ExportVo vo,HttpServletRequest req){
		String  loanTemplatePath = req.getSession().getServletContext().getRealPath("/downLoad");
		String userIp=this.getIPAddress(req);
		String fileName = fileService.exportExcel(vo, loanTemplatePath,userIp);
		JSONObject object=new JSONObject();
		if(fileName.equals("error")){
			object.put("result","false");
		}else{
			object.put("result","true");
			object.put("message",fileName);
		}
		return object.toJSONString();
	}
}
